package l_network;
// ==============================================
// URLConnection 클래스를 사용하여 원격 테스트 편집기
// make : pwg
// date : 2003
// note :
// ==============================================

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class UrlEx3 extends JFrame implements ActionListener
{

	myWindowAdapter myW;

	URL url;
	URLConnection  urlc;
	private JTextField textDomain;
	private JButton loadButton;
	private JButton saveButton;
	private JScrollPane scrollPane;
	private JTextArea textResult;

	public UrlEx3()
	{
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 633, 484);
		myW = new myWindowAdapter();
		addWindowListener(myW);

		getContentPane().setLayout(null);
		
		textDomain = new JTextField();
		textDomain.setBounds(12, 10, 285, 21);
		getContentPane().add(textDomain);
		textDomain.setColumns(10);
		
		loadButton = new JButton("LOAD");
		loadButton.setBounds(394, 9, 97, 23);
		getContentPane().add(loadButton);
		
		saveButton = new JButton("SAVE");
		saveButton.setBounds(493, 9, 97, 23);
		getContentPane().add(saveButton);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 41, 578, 384);
		getContentPane().add(scrollPane);
		
		textResult = new JTextArea();
		scrollPane.setViewportView(textResult);

		// titile
		try{
			String ip = InetAddress.getLocalHost().getHostAddress();
			setTitle("local IP : " + ip);
		}catch(Exception ex){}
		
		
		

	}

	// 원격 문서 저장
	// 원격 저장을 하려면 ftp 프로토콜을 사용해야 함.
	// ftp://id:pwd@domain/folder/filename
	// ftp를 사용하면 읽기 쓰기 모두 됨.
	public void saveProcess()
	{
		OutputStream output;
		OutputStreamWriter writer;
		PrintWriter print;
		
		try{
			url = new URL( textDomain.getText() ); // url 객체 생성
			urlc = url.openConnection(); // url 객체에서 urlc 객체 생성
			urlc.setDoOutput(true);
			
			output = urlc.getOutputStream();
			
			writer = new OutputStreamWriter(output);
			print = new PrintWriter(writer);
			
			
			print.println(textResult.getText());
			print.flush();
			
			print.close();
			writer.close();
			output.close();

		}catch(Exception ex){
			ex.printStackTrace();
		}

	}

	public void actionPerformed(ActionEvent e)
	{

	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UrlEx3 frame = new UrlEx3();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	class myWindowAdapter extends WindowAdapter
	{
		public void windowClosing(WindowEvent winEvent)
		{
			System.exit(0);
		}
	};


}
